﻿using System.Text.RegularExpressions;

namespace IOFileServer.ZMPhoneticEnglish.Implementation
{
    /// <summary>
    /// 实现类: 单词处理
    /// </summary>
    public class WordHandle : IWordHandle
    {
        private readonly Regex not_word_regex;

        public WordHandle()
        {
            not_word_regex = new Regex(@"[^a-zA-Z]", RegexOptions.ECMAScript | RegexOptions.Multiline);
        }

        bool IWordHandle.Compliance(string word, out string errorMsg)
        {
            if (string.IsNullOrEmpty(word) || string.IsNullOrWhiteSpace(word))
            {
                errorMsg = "单词为空";
                return false;
            }
            if (not_word_regex.IsMatch(word))
            {
                errorMsg = "存在非英文字母字符存在";
                return false;
            }
            errorMsg = string.Empty;
            return true;
        }

        string IWordHandle.Standard(string word)
        {
            if (string.IsNullOrEmpty(word) || string.IsNullOrWhiteSpace(word))
                return null;
            word = not_word_regex.Replace(word, "");
            word = word.ToLower().Trim();
            return word;
        }
    }
}
